package com.ea.service.impl;

import com.alibaba.fastjson.JSON;
import com.ea.common.enums.OperationResultCode;
import com.ea.common.enums.SysColumnTypeEnums;
import com.ea.common.enums.YesOrNoEnum;
import com.ea.common.exception.ServiceException;
import com.ea.common.util.IdWorker;
import com.ea.domain.dto.SysDicUpdateDTO;
import com.ea.domain.entity.SysBaseCfg;
import com.ea.domain.entity.SysBaseDictionary;
import com.ea.mapper.SysBaseCfgMapper;
import com.ea.mapper.SysBaseDictionaryMapper;
import com.ea.service.ISysBaseDictionaryService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author NAME-L
 * @Description TODO
 * @className ISysBaseDictionaryServiceImpl
 * @date 2023-02-01 23:39:24
 */
@Service
public class ISysBaseDictionaryServiceImpl implements ISysBaseDictionaryService {

    private static final Logger logger = LoggerFactory.getLogger(ISysBaseDictionaryServiceImpl.class);
    @Autowired
    private SysBaseDictionaryMapper baseDictionaryMapper;

    @Override
    public List<SysBaseDictionary> getSysDicList(String type) {
        if (StringUtils.isEmpty(type)) {
            throw new ServiceException(OperationResultCode.SYSTEM_ERROR);
        }
        boolean exists = SysColumnTypeEnums.ELECTRIC_TYPE.hasSysDicType(type);
        if (exists) {
            return baseDictionaryMapper.selectSysDicByDicType(type);
        }
        return new ArrayList<>();
    }

    @Override
    public void insertSysDic(SysBaseDictionary dictionary) {
        if (dictionary == null || StringUtils.isEmpty(dictionary.getDicType())) {
            throw new ServiceException(OperationResultCode.SYSTEM_ERROR);
        }
        String dicDesc = dictionary.getDicDesc();
        SysBaseDictionary sysBaseDictionary = baseDictionaryMapper.selectSysDicByDesc(dicDesc);
        if (sysBaseDictionary != null) {
            throw new ServiceException(OperationResultCode.SYS_BASE_DIC_EXISTS);
        }
        dictionary.setDicId(IdWorker.nextId());
        baseDictionaryMapper.insertDictionary(dictionary);
    }

    @Override
    public void updateSysDic(SysDicUpdateDTO updateDTO) {
        if (updateDTO == null) {
            throw new ServiceException(OperationResultCode.SYSTEM_ERROR);
        }
        String newDicDesc = updateDTO.getDicDesc();
        SysBaseDictionary newDictionary = baseDictionaryMapper.selectSysDicByDesc(newDicDesc);
        if (newDictionary != null && !newDicDesc.equals(newDictionary.getDicDesc())) {
            throw new ServiceException(OperationResultCode.SYS_BASE_DIC_UPDATE_ERROR);
        }

        SysBaseDictionary dictionary = baseDictionaryMapper.selectSysDicByDicId(updateDTO.getDicId());
        if (dictionary == null) {
            throw new ServiceException(OperationResultCode.SYS_BASE_DIC_UPDATE_ERROR);
        }
        if (YesOrNoEnum.YES.getCode().equals(dictionary.getIsSystem())) {
            throw new ServiceException(OperationResultCode.SYS_BASE_DIC_IS_SYSTEM);
        }
        baseDictionaryMapper.updateSysDic(dictionary.getDicId(), newDicDesc);
    }

    @Override
    public void delSysDic(Long dicId) {
        if (dicId == null) {
            throw new ServiceException(OperationResultCode.SYSTEM_ERROR);
        }
        SysBaseDictionary dictionary = baseDictionaryMapper.selectSysDicByDicId(dicId);
        if (dictionary == null) {
            return;
        }
        if (YesOrNoEnum.YES.getCode().equals(dictionary.getIsSystem())) {
            throw new ServiceException(OperationResultCode.SYS_BASE_DIC_IS_SYSTEM);
        }
        logger.info("删除数据字典：{}", JSON.toJSONString(dictionary));
        baseDictionaryMapper.delSysDicByDicId(dicId);

    }
}
